﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DevCow.EventServer.UI.WebControls
{
    [ToolboxData("<{0}:NextEvent runat=server HeaderColor=\"#C0C000\" width=\"100%\"></{0}:NextEvent>"), DefaultProperty("UserGroup")]
    public class NextEvent : WebControl, INamingContainer
    {
        // Fields
        private DataSet dsEvent;
        private TextBox txtUserName;

        // Methods
        public NextEvent()
        {
            this.txtUserName = new TextBox();
            this.dsEvent = new DataSet();
        }



        protected override void Render(HtmlTextWriter output)
        {
            EventServerComponents.UGEventServiceSoap service = new EventServerComponents.UGEventServiceSoapClient();
            
            if (this.UserGroup == "")
            {
                Label label = new Label {Text = "The user group has not been defined "};
                label.RenderControl(output);
            }
            else
            {
                try
                {
                    this.dsEvent = service.GetNextEvent(this.UserGroup, 1);
                }
                catch (Exception exception)
                {
                    Label label2 = new Label();
                    label2.Text = "Could not find www.devcow.com at the moment." + Environment.NewLine + exception.Message;
                    label2.RenderControl(output);
                    return;
                }
                if (this.dsEvent.Tables["UGEvents"].Rows.Count <= 0)
                {
                    Label label3 = new Label();
                    label3.Text = "There are no events on the event server for this user group";
                    label3.RenderControl(output);
                }
                else
                {
                    Table table = new Table();
                    table.Width = this.Width;
                    TableRow row = new TableRow();
                    TableCell cell = new TableCell();
                    cell.BackColor = this.HeaderColor;
                    Table child = new Table();
                    child.Width = Unit.Percentage(100.0);
                    TableRow row2 = new TableRow();
                    TableCell cell2 = new TableCell();
                    child.Rows.Add(row2);
                    row2 = new TableRow();
                    cell2 = new TableCell();
                    row2.HorizontalAlign = HorizontalAlign.Center;
                    cell2.Controls.Add(new LiteralControl(this.dsEvent.Tables["UGEvents"].Rows[0]["UserGroup"].ToString()));
                    cell2.Controls.Add(new LiteralControl("<BR>"));
                    cell2.Controls.Add(new LiteralControl("OUR NEXT MEETING:"));
                    row2.Cells.Add(cell2);
                    child.Rows.Add(row2);
                    row2 = new TableRow();
                    row2.HorizontalAlign = HorizontalAlign.Center;
                    cell2 = new TableCell();
                    DateTime time = (DateTime)this.dsEvent.Tables["UGEvents"].Rows[0]["Start"];
                    cell2.Controls.Add(new LiteralControl(time.ToString("dddd, MMMM d, yyyy @ h:mm tt")));
                    row2.Cells.Add(cell2);
                    child.Rows.Add(row2);
                    row2 = new TableRow();
                    row2.HorizontalAlign = HorizontalAlign.Center;
                    cell2 = new TableCell();
                    cell2.Controls.Add(new LiteralControl("<IMG height='5' src=''>"));
                    row2.Cells.Add(cell2);
                    child.Rows.Add(row2);
                    row2 = new TableRow();
                    row2.HorizontalAlign = HorizontalAlign.Center;
                    cell2 = new TableCell();
                    cell2.Controls.Add(new LiteralControl("<STRONG>Location: "));
                    cell2.Controls.Add(new LiteralControl(this.dsEvent.Tables["UGEvents"].Rows[0]["LocationName"].ToString()));
                    cell2.Controls.Add(new LiteralControl("</STRONG>"));
                    row2.Cells.Add(cell2);
                    child.Rows.Add(row2);
                    row2 = new TableRow();
                    row2.HorizontalAlign = HorizontalAlign.Center;
                    cell2 = new TableCell();
                    cell2.Controls.Add(new LiteralControl("<IMG height='5' src=''>"));
                    row2.Cells.Add(cell2);
                    child.Rows.Add(row2);
                    cell.Controls.Add(child);
                    row.Cells.Add(cell);
                    table.Rows.Add(row);
                    row = new TableRow();
                    cell = new TableCell();
                    cell.Controls.Add(new LiteralControl(this.dsEvent.Tables["UGEvents"].Rows[0]["Details"].ToString()));
                    row.Cells.Add(cell);
                    table.Rows.Add(row);
                    table.RenderControl(output);
                }
            }
        }

        // Properties
        [Editor("System.Drawing.Design.ColorEditor", "System.Drawing.Design.UITypeEditor"), Description("Display the color of the Header."), Category("UGEvent"), NotifyParentProperty(true), Bindable(true), TypeConverter(typeof(WebColorConverter))]
        public Color HeaderColor
        {
            get
            {
                if (this.ViewState["HeaderColor"] != null)
                {
                    return (Color)this.ViewState["HeaderColor"];
                }
                return Color.Empty;
            }
            set
            {
                this.ViewState["HeaderColor"] = value;
            }
        }


        [Browsable(true), Description("The UserGroup that is selected to display the next event"), Category("UGEvent"), DefaultValue(""), MergableProperty(false)]
        public string UserGroup
        {
            get
            {
                if (this.ViewState["UserGroup"] != null)
                {
                    return (string)this.ViewState["UserGroup"];
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["UserGroup"] = value;
            }
        }
 

    }

 

}
